<template>
  <div style="margin: 20px">
    <el-card>
      <div style="margin-bottom: 10px; display: flex">
        <el-input class="w-50 m-2" style="width: 150px" placeholder="请输入关键词" v-model="form.keywords" />
        <el-input class="w-50 m-2" style="width: 150px; margin-left: 10px" placeholder="请输入文件类型"
          v-model="form.extension" />
        <el-select style="width: 150px; margin-left: 10px;" placeholder="请选择标签" v-model="form.tagId" :multiple="false">
          <el-option value="0" label="所有">所有</el-option>
          <el-option v-for="(item, index) in tagList" :key="index" :label="item.tagName" :value="item.id"></el-option>
        </el-select>
        <el-button style="margin-left: 10px" type="primary" @click="get">搜索</el-button>
      </div>
      <el-table :data="state.tableData" :border="true" highlight-current-row>
        <el-table-column sortable prop="id" label="ID" width="65" />
        <el-table-column sortable prop="fileName" label="文件名称" />
        <el-table-column prop="fileTagsName" label="文件标签" width="150" >
          <template v-slot="scope">
            <el-tag class="ml-2" style="margin-top:2px;margin-bottom: 2px" v-for="item in scope.row.fileTagsName">{{ item }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column sortable prop="extension" label="扩展名" width="95" />
        <el-table-column prop="releaseCode" label="发文号" width="150" />
        <el-table-column sortable prop="startDate" label="启用日期" width="120" />
        <el-table-column sortable prop="expireDate" label="失效日期" width="120" />
        <el-table-column sortable prop="downloadCount" label="下载次数" width="105" />
        <el-table-column fixed="right" label="操作" width="150">
          <template v-slot="scope">
            <el-button link size="small" @click="download(scope.row.id)">下载</el-button>
            <el-button link type="primary" size="small" @click="censor(scope.row.id, 1)">通过</el-button>
            <el-button link type="danger" size="small" @click="doDel(scope.row.id, -1)">不通过</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="pagination" v-model:current-page="form.current" v-model:page-size="form.size"
        :page-sizes="[10, 20, 50]" :small="false" :disabled="false" :background="true"
        layout="total, sizes, prev, pager, next, jumper" :total="state.total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </el-card>
  </div>
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import router from "@/router";
import basisApi from "@/api/basis/basis";
import basisDict from "@/api/basis/dict";
import policyFilesApi from "@/api/basis/policy"
import policyTagApi from "@/api/basis/policyTag"

declare global {
  interface Date {
    Format(fmt: string): string;
  }
}

// 时间日期格式化
Date.prototype.Format = function (fmt: string) {
  // author: meizz
  var o = {
    "M+": this.getMonth() + 1, // 月份
    "d+": this.getDate(), // 日
    "h+": this.getHours(), // 小时
    "m+": this.getMinutes(), // 分
    "s+": this.getSeconds(), // 秒
    "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
    S: this.getMilliseconds(), // 毫秒
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(
      RegExp.$1,
      (this.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  for (let k in o)
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
      );
  return fmt;
};

// type definition
type project = {
  id: number;
  name: string;
  type: number;
  createTime: Date;
  description: string;
};

// tableData
const form = reactive({
  current: 1,
  keywords: "",
  censorStat: 0,
  extension: "",
  tagId: 0,
  size: 10,
});
const state = reactive({
  total: 0,
  tableData: [] as Array<project>,
});
const handleSizeChange = (val: number) => {
  form.size = val;
  get();
};
const handleCurrentChange = (val: number) => {
  form.current = val;
  get();
};
const get = () => {
  policyFilesApi.get(form).then((res) => {
    if (res.data.code === 200) {
      state.tableData = res.data.data.records;
      state.total = res.data.data.total;
    }
  });
};
const download = (id: number) => {
  // policyFilesApi.download(id).then((res) => {
  //   console.log(res)
  // })
  window.open(`http://localhost:8001/policy/download/${id}`);
}

const censor = (id: number, stat: number) => {
  policyFilesApi.censor(id, stat).then((res) => {
    if (res.status === 200) {
      ElMessage.success("操作成功");
      get();
    }
  })
}

const doDel = (id: number) => {
  policyFilesApi.del(id).then((res) => {
    if (res.status === 200) {
      ElMessage.success("删除成功");
      get();
    }
  });
};

// 标签列表
const tagList = ref([] as Array<any>)

const getTag = () => {
  policyTagApi.get().then((res) => {
    tagList.value = res.data.data
  })
}

onMounted(() => {
  getTag();
  get();
});
</script>
<style lang="scss" scoped>
.tabs>.el-tabs__content {
  width: 100%;
  padding: 32px;
  color: #6b778c;
  font-size: 32px;
  font-weight: 600;
}

.tabs {
  margin: 20px;
}
</style>
<!-- 蚌埠 -->